/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.common.util.file;

import java.io.File;
import java.io.IOException;

/**
 * @author PC (Tran Nguyen Minh Khoi)
 */
public class FileMovingUtils {

    public static File[] toArrFile(String[] listFilePath) {
	File[] rs = new File[listFilePath.length];
	for (int i = 0; i < listFilePath.length; i++) {
	    String fpath = listFilePath[i];
	    File f = new File(fpath);
	    rs[i] = f;
	}
	return rs;
    }

    /**
     * Only copy the src to des. NOT copy sub files and sub directories of src.
     *
     * @param srcDir source file
     * @param desDir destination file
     * @param override if override == false and destination has already existed,
     * skip it (do nothing).
     * @throws IOException NullPointerException If des or src is null
     */
    public static void copyOnePath(File src, File des, boolean override) throws IOException {
	if (des.exists() && override == false) {
	    return;
	}
	org.apache.commons.io.FileUtils.copyFile(src, des);
    }

    /**
     * Create a file, NOT make a directory!
     *
     * @param path If parent directory is not exist: make dirs, and then create
     * file
     * @throws IOException
     */
    public static void createFileTree(File file) throws IOException {
	if (file.exists()) {
	    return;
	}
	File parentDir = file.getParentFile();
	if (parentDir != null && !parentDir.exists()) {
	    parentDir.mkdirs();
	}
	file.createNewFile();
    }


    // <editor-fold defaultstate="collapsed" desc="DelTree">
    public static boolean delTree(File dir) {
	File[] listFile = dir.listFiles();
	for (File ifile : listFile) {
	    if (ifile.isDirectory()) {
		delTree(ifile);
	    } else {
		ifile.delete();
	    }
	}
	return dir.delete();
    }
    // </editor-fold>

}
